local thinkInterval = 1
local Init_Path = {1,2,3,4,5,6,7,8}

function Spawn( entityKeyValues )
	thisEntity:SetContextThink( "AIThink", AIThink, 0.25 )
	thisEntity:SetContextThink(DoUniqueString("GetPlayer"), function()

	local player_id = thisEntity._playerID
	
	local player = PlayerResource:GetPlayer(thisEntity._playerID)
	
	
	
	--初始化单位行进路线
	if player.is_alignment then -- 如果是对线
		thisEntity.cur_path_index = alignment[player_id+1]
	else --不是对线
		if player.is_clockwise  then --如果为顺时针
			thisEntity.cur_path_index =  (player_id + 2) % (GameRules.SGX_PLAYER_COUNT + 1)
		else --逆时针
			thisEntity.cur_path_index =  (player_id + 8) % (GameRules.SGX_PLAYER_COUNT + 1)
		end
	end
	

	end, 0)
	
end
alignment = {
	[1] = 5,
	[2] = 4,
	[3] = 7,
	[4] = 2,
	[5] = 1,
	[6] = 8,
	[7] = 3,
	[8] = 1
}
function AIThink()
	local player = PlayerResource:GetPlayer(thisEntity._playerID)
	local PlayerID = thisEntity._playerID
	--print(player,PlayerID)
	if player == nil or not IsValidEntity(player) then return nil end
	print("cur_path_index",thisEntity.cur_path_index)
	--print("last_path_index",thisEntity.last_path_index)
	if  (thisEntity:GetOrigin() - Entities:FindByName(nil, "PlayerSpawner"..tostring(thisEntity.cur_path_index)):GetOrigin()):Length() <= 300 then

		if player.is_clockwise  then --如果为顺时针
			thisEntity.cur_path_index =  (thisEntity.cur_path_index + 1) % (GameRules.SGX_PLAYER_COUNT + 1)
			if thisEntity.cur_path_index == 0 then thisEntity.cur_path_index = 1 end
		else --逆时针
			thisEntity.cur_path_index =  (thisEntity.cur_path_index  + 8) % (GameRules.SGX_PLAYER_COUNT + 1) 
			
			
			if thisEntity.cur_path_index == 0 then thisEntity.cur_path_index = 8    end
		end
	else
		local newOrder = {
		UnitIndex = thisEntity:entindex(), 
 		OrderType = DOTA_UNIT_ORDER_ATTACK_MOVE,
 		TargetIndex = nil, --Optional.  Only used when targeting units
 		AbilityIndex = 0, --Optional.  Only used when casting abilities
 		Position = Entities:FindByName(nil, "PlayerSpawner"..tostring(thisEntity.cur_path_index)):GetOrigin(), --Optional.  Only used when targeting the ground
 		Queue = 0 --Optional.  Used for queueing up abilities
	 	}
		ExecuteOrderFromTable(newOrder)
	end
	return thinkInterval
end
